package stego.StegoCaller;

import java.io.*;
import javax.swing.JTextPane;
import stego.Drivers;

public class Caller_UploadContent
{

    private static final String strMyClassName = "Caller_UploadContent";

    public Caller_UploadContent(boolean includeIndexNumberInOutPut, File pathTo_JAR, File filesToUpload[], String userName, String password, String tag, int accountTypeIndex, 
            JTextPane jtextPaneOutput, boolean useNativeAPI, boolean useCustInstr, boolean enterExtCmds, String extCmds)
    {
        processCommand(includeIndexNumberInOutPut, pathTo_JAR, filesToUpload, userName, password, tag, accountTypeIndex, jtextPaneOutput, useNativeAPI, useCustInstr, enterExtCmds, extCmds);
    }

    public boolean processCommand(boolean includeIndexNumber, File flePathTo_JAR, File flePathsTo_Upload[], String usrName, String pwd, String tg, int acctTypeIdx, 
            JTextPane jtxtpneOutput, boolean useNativeInstructions, boolean useCustInstructions, boolean enterExternalCommands, String externalCmds)
    {
        if(flePathsTo_Upload == null || flePathsTo_Upload.length < 1)
        {
            Drivers.jop_Error("Array of Selected Files to Upload was not populated.", "Critical Error");
            return false;
        }
        try
        {
            if(useNativeInstructions)
            {
                for(int i = 0; i < flePathsTo_Upload.length; i++)
                {
                    if(flePathsTo_Upload[i] != null && flePathsTo_Upload[i].exists() && flePathsTo_Upload[i].isFile())
                    {
                        processUpload_UseInstructions(i, includeIndexNumber, flePathTo_JAR, flePathsTo_Upload[i], usrName, pwd, tg, acctTypeIdx, jtxtpneOutput, false, null);
                    }
                }

            } else
            if(useCustInstructions)
            {
                for(int i = 0; i < flePathsTo_Upload.length; i++)
                {
                    if(flePathsTo_Upload[i] != null && flePathsTo_Upload[i].exists() && flePathsTo_Upload[i].isFile())
                    {
                        processUpload_UseInstructions(i, includeIndexNumber, flePathTo_JAR, flePathsTo_Upload[i], usrName, pwd, tg, acctTypeIdx, jtxtpneOutput, false, null);
                    }
                }

            } else
            if(enterExternalCommands)
            {
                for(int i = 0; i < flePathsTo_Upload.length; i++)
                {
                    if(flePathsTo_Upload[i] != null && flePathsTo_Upload[i].exists() && flePathsTo_Upload[i].isFile())
                    {
                        processUpload_UseInstructions(i, includeIndexNumber, flePathTo_JAR, flePathsTo_Upload[i], usrName, pwd, tg, acctTypeIdx, jtxtpneOutput, enterExternalCommands, externalCmds);
                    }
                }

            }
        }
        catch(Exception e)
        {
            Drivers.eop("processCommand", "Caller_UploadContent", e, e.getLocalizedMessage(), true);
            return false;
        }
        return true;
    }

    private boolean processUpload_UseInstructions(int myIndexNumber, boolean appendIndexToTag, File flePathToJAR, File flePathImageToUpload, String strUserName, String strPassword, String strTag, 
            int accountTypeIndex, JTextPane jtxtpneOut, boolean useExternalCommand, String strExternalCommand)
    {
        try
        {
            String strJAR_Name = flePathToJAR.getCanonicalPath().substring(flePathToJAR.getCanonicalPath().lastIndexOf(System.getProperty("file.separator")) + 1);
            String strCommand = (new StringBuilder("java -jar \"")).append(flePathToJAR.getCanonicalPath()).append("\"").append(" -u ").append(strUserName).append(" ").append(strPassword).append(" ").append("\"").append(flePathImageToUpload.getAbsolutePath()).append("\"").append(" ").append("\"").append(strTag).append("\"").toString();
            if(appendIndexToTag)
            {
                strCommand = (new StringBuilder(String.valueOf(strCommand))).append("_").append(myIndexNumber).toString();
            }
            if(useExternalCommand && strExternalCommand != null)
            {
                strCommand = strExternalCommand;
            }
            if(strCommand != null && !strCommand.trim().toUpperCase().startsWith("JAVA"))
            {
                Drivers.jop_Warning((new StringBuilder("WARNING: The following command: ")).append(strCommand).append("\nDoes not appear to be within expected parameters to launch a JAR file").toString(), "Unverified Command Received");
            }
            Drivers.appendTextToJTextPane(jtxtpneOut, (new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString(), true);
            Drivers.sop((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString());
            Process p = Runtime.getRuntime().exec((new StringBuilder("cmd /c ")).append(strCommand).toString());
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            for(String line = ""; (line = input.readLine()) != null;)
            {
                Drivers.sop(line);
                Drivers.appendTextToJTextPane(jtxtpneOut, line, true);
                Drivers.sop(line);
            }

            Drivers.sop("\n\n");
            Drivers.appendTextToJTextPane(jtxtpneOut, "\n\n", true);
        }
        catch(Exception e)
        {
            Drivers.eop("processUpload_UseNativeInstructions", "Caller_UploadContent", e, e.getLocalizedMessage(), true);
            Drivers.sop("\n\n");
            Drivers.appendTextToJTextPane(jtxtpneOut, "\n\n", true);
            return false;
        }
        return true;
    }
}
